package com.servlet;

import com.dao.CateGoryDao;
import com.dao.Impl.*;
import com.entity.*;
import com.util.MyUtil;
import com.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 沐白
 */
@WebServlet(value = "/procurementController/*")
public class ProcurementServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String methon = MyUtil.getMethon(request);
        switch (methon) {
            case "show":
                show(request, response);
                break;
            case "showInfo":
                showInfo(request, response);
                break;
            case "ruku":
                ruku(request, response);
                break;
            default:
                break;
        }
    }

    private void showInfo(HttpServletRequest request, HttpServletResponse response) {
        Integer storageId = Integer.valueOf(request.getParameter("storageId"));
        Storage storage = new StorageDaoImpl().findByStorageId(storageId);
        storage.setStorageinfoList(new StorageinfoDaoImpl().findBystorageId(storageId));
        for (Storageinfo storageinfo : storage.getStorageinfoList()) {
            storageinfo.setGoods(new GoodsDaoImpl().findByGoodsId(storageinfo.getGoodsId()));
        }
        MyUtil.jsonWriter(response, storage);
    }

    private void ruku(HttpServletRequest request, HttpServletResponse response) {
        GoodsDaoImpl goodsDao = new GoodsDaoImpl();
        Integer storageId = Integer.valueOf(request.getParameter("storageId"));
        String goodsName = request.getParameter("goodsName");
        String goodsCount = request.getParameter("goodsCount");
        if (goodsName != null && !goodsName.equals("")) {
            String[] name = goodsName.split(",");
            String[] count = goodsCount.split(",");
            int sint = 0;
            sint = new StorageDaoImpl().updateStatus(storageId,2);
            for (int i = 0; i < name.length; i++) {
                goodsDao.updateByNameCount(name[i], Integer.parseInt(count[i]));
                goodsDao.updateByNameNumstatus(name[i]);
            }
            MyUtil.jsonWriter(response, sint);
        } else {
            MyUtil.jsonWriter(response, -1);
        }
    }


    private void show(HttpServletRequest request, HttpServletResponse response) {
        PageBean<Storage> pageBean = new PageBean<>();
        TransBean.populate(pageBean, request.getParameterMap());
        new StorageDaoImpl().show(pageBean);
        pageBean.setStartIndex();
        List<Storage> storages = pageBean.getData();
        if (storages.size() > 0 && storages != null) {
            for (Storage storage : storages) {
                if(storage.getStaffId()!=null){
                    storage.setStaff(new StaffDaoImpl().findStaffId(storage.getStaffId()));
                }
            }
        }
        MyUtil.jsonWriter(response, pageBean);
    }
}
